package com.example.weather_app.core;

import static com.example.weather_app.PhotoSelect.what_weather;

import android.content.Context;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;
import androidx.annotation.NonNull;
import androidx.recyclerview.widget.RecyclerView;
import com.example.weather_app.core.ForecastDayData;
import com.example.weather_app.R;
import java.util.List;

public class ForecastAdapter extends RecyclerView.Adapter<ForecastAdapter.ForecastViewHolder> {

    private List<ForecastDayData> forecastList;

    public ForecastAdapter(List<ForecastDayData> forecastList) {
        this.forecastList = forecastList;
    }

    @NonNull
    @Override
    public ForecastViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.item_forecast, parent, false);
        return new ForecastViewHolder(view);
    }


    @Override
    public void onBindViewHolder(@NonNull ForecastViewHolder holder, int position) {
        ForecastDayData dayData = forecastList.get(position);
        holder.tvDate.setText(dayData.getDate());
        holder.tvweek.setText(dayData.getWeather());
        holder.tvlowtemp.setText(dayData.getLowTemp().replace("低温 ", ""));
        holder.tvhightemp.setText(dayData.getHighTemp().replace("高温 ", ""));
        holder.weather.setImageResource(what_weather(dayData.getWeather()));
    }

    @Override
    public int getItemCount() {
        return forecastList.size();
    }

    public static class ForecastViewHolder extends RecyclerView.ViewHolder {
        TextView tvDate;

        ImageView weather;
        TextView tvweek;

        TextView tvlowtemp;

        TextView tvhightemp;

        public ForecastViewHolder(@NonNull View itemView) {
            super(itemView);
            tvDate = itemView.findViewById(R.id.item_date);
            weather=itemView.findViewById(R.id.item_weather_icon);
            tvweek=itemView.findViewById(R.id.item_week);
            tvlowtemp=itemView.findViewById(R.id.item_low_temp);
            tvhightemp=itemView.findViewById(R.id.item_high_temp);

        }
    }
}